package com.isoftstone.disk.user.controller;

import com.alibaba.fastjson.JSON;
import com.isoftstone.disk.login.entity.PtUserEntity;
import com.isoftstone.disk.mybatis.flexigrid.PadingRstType;
import com.isoftstone.disk.mybatis.flexigrid.PagingBean;
import com.isoftstone.disk.mybatis.ztree.ZtreeEntity;
import com.isoftstone.disk.spring.controller.BaseController;
import com.isoftstone.disk.spring.security.GdmtPasswordEncoder;
import com.isoftstone.disk.user.entity.SearUserEntity;
import com.isoftstone.disk.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    private GdmtPasswordEncoder gdmtPasswordEncoder;
    private Logger logger = LogManager.getLogger(this.getClass().getName());
    @Autowired
    private UserService userService;
    @Secured("ROLE_cldk_data_user")
    @RequestMapping("/userPage")
    public String userPage(){
        return "user/userPage";
    }

    @RequestMapping("/getUserInfolistByPage")
    @ResponseBody
    public String getUserInfolistByPage(SearUserEntity searUserEntity, PagingBean pagingBean){
        logger.info(searUserEntity);
        logger.info(pagingBean);
        PadingRstType<PtUserEntity> ptUserEntityPadingRstType = userService.getUserInfolistByPage(searUserEntity,pagingBean);

        return JSON.toJSONString(ptUserEntityPadingRstType);
    }
    @RequestMapping("/queryroleZtreeList")
    @ResponseBody
    public List<ZtreeEntity> queryroleZtreeList(@RequestParam("userUuid") String userUuid){
        logger.info("userUuid:" + userUuid);
        List<ZtreeEntity> list  =userService.queryroleZtreeList(userUuid);
        return  list;
    }


    @RequestMapping("/saveRefRoleIdByUserId")
    @ResponseBody
    public String saveRefRoleIdByUserId(@RequestParam("roleIds")String roleIds,@RequestParam("userId")String userId){
        logger.info("roleIds:" + roleIds + " userId:" + userId);
        userService.saveRefRoleIdByUserId(roleIds,userId);
        return jsonSuccess("user.allocat.role.success");

    }

    @Value("${passwd.key}")
    private String passwdKey;
    @ResponseBody
    @PostMapping("/insertUserInfo")
    public Object add(PtUserEntity user) {
//        logger.info(user);
//        return jsonSuccess("user.allocat.role.success");
        user.setDelFlag(0);
        user.setPassword(gdmtPasswordEncoder.encode(user.getPassword()) );
        if (userService.findByUserName(user.getUserName()) != null) {
            return jsonSuccess("user.re.live");

        } else {
            userService.addUser(user);
            return jsonSuccess("user.re.success");
        }
    }

}
